//
//  TTEmoticonView.swift
//  TTProduct
//
//  Created by Toj on 3/29/21.
//  Copyright © 2021 TTProduct. All rights reserved.
//

import Cocoa

@objc protocol TTEmoticonViewDelegate : NSObjectProtocol {
    @objc optional func emojiViewDidTapSelected(data : TTEmoticon?)
}

class TTEmoticonView: NSView {
    
    public weak var delegate: TTEmoticonViewDelegate?
    
    private let rowCount = 5
    
    private var emoticons: [TTEmoticon]? {
        didSet {
            collectionView.reloadData()
        }
    }
    
    private lazy var scrollView: NSScrollView = {
        let view = NSScrollView()
        view.scrollerStyle = .overlay
        view.scrollerKnobStyle = .dark
        view.verticalScrollElasticity = .automatic
        view.documentView = collectionView
        view.tt_shadow(color: NSColor.black.tt_alpha(0.3))
        return view
    }()
    
    internal let emotionIdentifier = NSUserInterfaceItemIdentifier(rawValue: TTEmotionItem.className())
    private lazy var collectionView: NSCollectionView = {
        let view = NSCollectionView()
        view.dataSource = self
        view.delegate = self
        view.isSelectable = true
        //view.allowsMultipleSelection = true
        view.backgroundColors = [NSColor.white]
        
        let flowLayout = NSCollectionViewFlowLayout()
        flowLayout.scrollDirection = .horizontal
        view.collectionViewLayout = flowLayout
        
        view.register(TTEmotionItem.self, forItemWithIdentifier: emotionIdentifier)
        return view
    }()
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)

        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
        setupMakeUI()
        
        setupMakeData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        print(#function + className)
    }
}

// MARK: - Public
extension TTEmoticonView {
    
}

// MARK: - Private
extension TTEmoticonView {
    
}

// MARK: - 点击事件
extension TTEmoticonView {
    
}

// MARK: - collection
extension TTEmoticonView: NSCollectionViewDataSource {
    
    func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
        return emoticons?.count ?? 0
    }
    
    func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
        let cell = collectionView.makeItem(withIdentifier: emotionIdentifier, for: indexPath) as! TTEmotionItem
        cell.emoticon = emoticons?[indexPath.item]
        return cell
    }
}

extension TTEmoticonView: NSCollectionViewDelegate {
    
    func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
        
        let item = collectionView.item(at: indexPaths.first!) as? TTEmotionItem
        delegate?.emojiViewDidTapSelected?(data: item?.emoticon)
        
        collectionView.deselectItems(at: indexPaths)
    }
    
    func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
        print(#function)
    }
}

extension TTEmoticonView: NSCollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
        let width : CGFloat = (collectionView.bounds.width - 4) / CGFloat(rowCount)
        let height = bounds.height
        return NSSize(width: width, height: height)
    }
    
    func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
        return NSEdgeInsets(top: 0, left: 2, bottom: 0, right: 2)
    }

    func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0.01
    }

    func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0.01
    }
}

// MARK: - setup
extension TTEmoticonView {
    
    private func setupMakeAddSubviews() {
        addSubview(scrollView)
    }
    
    private func setupMakeLayoutSubviews() {
        scrollView.snp.makeConstraints { (make) in
            make.left.right.top.bottom.equalTo(self)
        }
    }
    
    private func setupMakeUI() {
        tt_backgroundColor = .white
    }
    
    private func setupMakeData() {
        emoticons = TTEmoticonSource(source: "Emoticons", ext: "bundle").path(resource: "Emoticons.plist", ofType: nil)
    }
}
